package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.statistics;

import java.text.DecimalFormat;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.CompanyWeightMoneyNumBo;

import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.GetWeightByMonthBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CIcCardsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CPdtClass;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CProductsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.CompanyWeightMoneyNumExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.ProductWeightMoneyNumExportExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICIcCardsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICPdtClassService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICWeightSheetsService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.CompanyWeightMoneyNumVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetWeightByDayVo;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xxl.job.core.util.DateUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class CompanyWeightMoneyNumBu {
    @Autowired
    private ICWeightSheetsService weightSheetsService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ICPdtClassService icPdtClassService;
    @Autowired
    private ICProductsInfoService icProductsInfoService;
    @Autowired
    private ICIcCardsInfoService iCIcCardsInfoService;
//按照单位查询

    /**
     * 按照单位查询
     *
     * @param startTime:开始时间
     * @param endTime        :结束时间
     * @param coSupply       :供货单位
     * @param co             :收货单位
     * @param product        :品种
     * @param truckNo        :车牌号
     * @param iccardno       :卡号
     * @param isEmpty        :是否空车
     * @param cpyClass       :单位类型
     * @param pdtClass       :品种类型
     * @return
     */
    public SzResult getcompanyWeightMoneyNum(String startTime, String endTime, String coSupply, String coSupplyChi, String co, String chiCo, Integer pdtClassId, List<String> product, String truckNo, String iccardno,String iccardAlias, Integer isEmpty, String cpyClass, String pdtClass, String token, String plantId) throws Exception {
        //  String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
//        判空
        if (coSupply != null && !coSupply.equals("")) {
            companyWeightMoneyNumBO.setCoSupply(coSupply);
        }
        if (coSupplyChi != null && !coSupplyChi.equals("")) {
            companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
        }
        if (co != null && !co.equals("")) {
            companyWeightMoneyNumBO.setCo(co);
        }
        if (chiCo != null && !chiCo.equals("")) {
            companyWeightMoneyNumBO.setChiCo(chiCo);
        }
        if (pdtClassId != null) {
            QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
            cPdtClassQueryWrapper.eq("PDTCLASS_ID", pdtClassId);
//            PDTCLASS_DELETED=0
            cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
            cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
            List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
            List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getPdtclassId).collect(Collectors.toList());
            QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
            cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
            List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
            companyWeightMoneyNumBO.setProduct(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
        }
        if (product != null && product.size() > 0) {
            companyWeightMoneyNumBO.setProduct(product);
        }
        if (startTime != null && !startTime.equals("")) {
            companyWeightMoneyNumBO.setStartTime(startTime);
        }
        if (endTime != null && !endTime.equals("")) {
            companyWeightMoneyNumBO.setEndTime(endTime);
        }
        if (truckNo != null && !truckNo.equals("")) {
            companyWeightMoneyNumBO.setTruckNo(truckNo);
        }
        if (iccardno != null && !iccardno.equals("")) {
            companyWeightMoneyNumBO.setIccardno(iccardno);
        }
        if (iccardAlias !=null && !iccardAlias.equals("")) {
            QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("CARDINF_DELETED", 0);
            queryWrapper3.eq("DATA_FROM", plantId);
            queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
            List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
            if (cIcCardsInfos.size()>0) {
                List<String> list = new ArrayList<>();
                for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                    list.add(cIcCardsInfo.getCardinfCardno());
                }
                companyWeightMoneyNumBO.setIccardnoAlias(list);
            }
        }
        if (isEmpty != null) {
            companyWeightMoneyNumBO.setIsEmpty(isEmpty);
        }
        if (plantId != null && !plantId.equals("")) {
            companyWeightMoneyNumBO.setPlantId(plantId);
        }
//        默认为不查询空车
        else {
            companyWeightMoneyNumBO.setIsEmpty(0);
        }

        // 获取查询结果
        List<CompanyWeightMoneyNumVo> companyWeightMoneyNumList = weightSheetsService.companyWeightMoneyNum(companyWeightMoneyNumBO);

// 计算合计值
        BigDecimal totalWeight = BigDecimal.ZERO;
        BigDecimal totalMoney = BigDecimal.ZERO;
        BigDecimal totalNum = BigDecimal.ZERO;

        for (CompanyWeightMoneyNumVo vo : companyWeightMoneyNumList) {
            if (vo.getTotalWeight() != null) {
                totalWeight = totalWeight.add(vo.getTotalWeight());
            }
            if (vo.getTotalMoney() != null) {
                totalMoney = totalMoney.add(vo.getTotalMoney());
            }
            if (vo.getTotalNum() != null) {
                totalNum = totalNum.add(vo.getTotalNum());
            }
        }
        // 创建合计对象并添加到列表
        CompanyWeightMoneyNumVo totalVo = new CompanyWeightMoneyNumVo();
        totalVo.setCo("合计");
        totalVo.setTotalWeight(totalWeight);
        totalVo.setTotalMoney(totalMoney);
        totalVo.setTotalNum(totalNum);
        // 其他字段置空或保持默认值

        // 将合计对象添加到结果列表中
        companyWeightMoneyNumList.add(totalVo);

        return SzResult.success(companyWeightMoneyNumList);
//        return SzResult.success(companyWeightMoneyNum);
    }
//    按照品种查询
    /**
     * 按照品种查询
     * @param startTime      :开始时间
     * @param endTime        :结束时间
     * @param coSupply       :供货单位
     * @param co             :收货单位
     * @param product        :品种
     * @param truckNo        :车牌号
     * @param iccardno       :卡号
     * @param isEmpty        :是否空车
     * @param cpyClass       :单位类型
     * @param pdtClass       :品种类型
     * @return
     */
    public SzResult getproductWeightMoneyNum(String startTime, String endTime, String coSupply, String coSupplyChi, String co, String chiCo, Integer pdtClassId, List<String> product, String truckNo, String iccardno,String iccardAlias, Integer isEmpty, String cpyClass, String pdtClass, String token, String plantId) throws Exception {
        //    传入日期，按品种查询这期间每个公司的总重量，总金额，总条数。如果有时间传入，就按时间查询，如果没有就查询全部；如果有公司传入，就按公司查询，如果没有就查询全部；如果有品种传入，就按品种查询，如果没有就查询全部
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
//        判空
        if (coSupply != null && !coSupply.equals("")) {
            companyWeightMoneyNumBO.setCoSupply(coSupply);
        }
        if (coSupplyChi != null && !coSupplyChi.equals("")) {
            companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
        }
        if (co != null && !co.equals("")) {
            companyWeightMoneyNumBO.setCo(co);
        }
        if (chiCo != null && !chiCo.equals("")) {
            companyWeightMoneyNumBO.setChiCo(chiCo);
        }
        if (pdtClassId != null) {
            QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
            cPdtClassQueryWrapper.eq("PDTCLASS_ID", pdtClassId);
//            PDTCLASS_DELETED=0
            cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
            cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
            List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
            List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getPdtclassId).collect(Collectors.toList());
            QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
            cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
            List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
            companyWeightMoneyNumBO.setProduct(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
        }
        if (product != null && product.size() > 0) {
            companyWeightMoneyNumBO.setProduct(product);
        }
        if (startTime != null && !startTime.equals("")) {
            companyWeightMoneyNumBO.setStartTime(startTime);
        }
        if (endTime != null && !endTime.equals("")) {
            companyWeightMoneyNumBO.setEndTime(endTime);
        }
        if (truckNo != null && !truckNo.equals("")) {
            companyWeightMoneyNumBO.setTruckNo(truckNo);
        }
        if (iccardno != null && !iccardno.equals("")) {
            companyWeightMoneyNumBO.setIccardno(iccardno);
        }

        if (isEmpty != null) {
            companyWeightMoneyNumBO.setIsEmpty(isEmpty);
        }
        if (plantId != null && !plantId.equals("")) {
            companyWeightMoneyNumBO.setPlantId(plantId);
        }
//        默认为不查询空车
        else {
            companyWeightMoneyNumBO.setIsEmpty(0);
        }
        if (iccardAlias !=null && !iccardAlias.equals("")) {
            QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("CARDINF_DELETED", 0);
            queryWrapper3.eq("DATA_FROM", plantId);
            queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
            List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
            if (cIcCardsInfos.size()>0) {
                List<String> list = new ArrayList<>();
                for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                    list.add(cIcCardsInfo.getCardinfCardno());
                }
                companyWeightMoneyNumBO.setIccardnoAlias(list);
            }
        }
        //coSupply=null, coSupplyChi=null, co=null, chiCo=null, iccardno=null, product=[#1、2细灰], startTime=null, endTime=null, truckNo=null, isEmpty=0, plantId=e9f6ed8e091611ec842700163e092c06, pdtClassId=null
        // 获取查询结果
        List<CompanyWeightMoneyNumVo> companyWeightMoneyNumList = weightSheetsService.productWeightMoneyNum(companyWeightMoneyNumBO);

// 计算合计值
        BigDecimal totalWeight = BigDecimal.ZERO;
        BigDecimal totalMoney = BigDecimal.ZERO;
        BigDecimal totalNum = BigDecimal.ZERO;

        for (CompanyWeightMoneyNumVo vo : companyWeightMoneyNumList) {
            if (vo.getTotalWeight() != null) {
                totalWeight = totalWeight.add(vo.getTotalWeight());
            }
            if (vo.getTotalMoney() != null) {
                totalMoney = totalMoney.add(vo.getTotalMoney());
            }
            if (vo.getTotalNum() != null) {
                totalNum = totalNum.add(vo.getTotalNum());
            }
        }

        // 创建合计对象并添加到列表
        CompanyWeightMoneyNumVo totalVo = new CompanyWeightMoneyNumVo();
        totalVo.setTotalWeight(totalWeight);
        totalVo.setTotalMoney(totalMoney);
        totalVo.setTotalNum(totalNum);
        // 其他字段置空或保持默认值
        totalVo.setPdtClass(null);
        totalVo.setProduct("合计");
        totalVo.setCo(null);
        totalVo.setChiCo(null);
        totalVo.setCoSupply(null);
        totalVo.setCoSupplyChi(null);

        // 将合计对象添加到结果列表中
        companyWeightMoneyNumList.add(totalVo);
        return SzResult.success(companyWeightMoneyNumList);
    }

    /**
     * 按照品种查询
     *
     * @param startTime:开始时间
     * @param endTime        :结束时间
     * @param coSupply       :供货单位
     * @param co             :收货单位
     * @param product        :品种
     * @param truckNo        :车牌号
     * @param iccardno       :卡号
     * @param isEmpty        :是否空车
     * @return
     */
    public SzResult productWeightMoneyNumByOne(String startTime, String endTime, String coSupply, String coSupplyChi, String co, String chiCo, List<String> product, String truckNo, String iccardno,String iccardAlias, Integer isEmpty, String token, String plantId) throws Exception {
        //    传入日期，按品种查询这期间每个公司的总重量，总金额，总条数。如果有时间传入，就按时间查询，如果没有就查询全部；如果有公司传入，就按公司查询，如果没有就查询全部；如果有品种传入，就按品种查询，如果没有就查询全部
        // String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
        if (coSupply != null && !coSupply.equals("")) {
            companyWeightMoneyNumBO.setCoSupply(coSupply);
        }
        if (coSupplyChi != null && !coSupplyChi.equals("")) {
            companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
        }
        if (co != null && !co.equals("")) {
            companyWeightMoneyNumBO.setCo(co);
        }
        if (chiCo != null && !chiCo.equals("")) {
            companyWeightMoneyNumBO.setChiCo(chiCo);
        }
        if (product != null && product.size() > 0) {
            companyWeightMoneyNumBO.setProduct(product);
        }
        if (startTime != null && !startTime.equals("")) {
            companyWeightMoneyNumBO.setStartTime(startTime);
        }
        if (endTime != null && !endTime.equals("")) {
            companyWeightMoneyNumBO.setEndTime(endTime);
        }
        if (truckNo != null && !truckNo.equals("")) {
            companyWeightMoneyNumBO.setTruckNo(truckNo);
        }
        if (iccardno != null && !iccardno.equals("")) {
            companyWeightMoneyNumBO.setIccardno(iccardno);
        }

        if (isEmpty != null) {
            companyWeightMoneyNumBO.setIsEmpty(isEmpty);
        }
        if (plantId != null && !plantId.equals("")) {
            companyWeightMoneyNumBO.setPlantId(plantId);
        }
//        默认为不查询空车
        else {
            companyWeightMoneyNumBO.setIsEmpty(0);
        }
        if (iccardAlias !=null && !iccardAlias.equals("")) {
            QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("CARDINF_DELETED", 0);
            queryWrapper3.eq("DATA_FROM", plantId);
            queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
            List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
            if (cIcCardsInfos.size()>0) {
                List<String> list = new ArrayList<>();
                for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                    list.add(cIcCardsInfo.getCardinfCardno());
                }
                companyWeightMoneyNumBO.setIccardnoAlias(list);
            }
        }
        List<CompanyWeightMoneyNumVo> companyWeightMoneyNum = weightSheetsService.productWeightMoneyNumByOne(companyWeightMoneyNumBO);
        return SzResult.success(companyWeightMoneyNum);
    }

    //    按单位查询数据导出excel
    @Async
    public Future<List<CompanyWeightMoneyNumExcelEn>> companyWeightMoneyNumExportExcel(List<Map<String, Object>> fields, String filename, List<Map<String, Object>> ids, Boolean isHeader, String mode, Boolean original, Map<String, String> pager, String sheetName, String token, String startTime, String endTime, String coSupply, String coSupplyChi, String co, String chiCo, Integer pdtClassId, List<String> product, String truckNo, String iccardno,String iccardAlias, Integer isEmpty, String cpyClass, String pdtClass, String plantId) throws Exception {
        //    传入日期，按单位查询这期间每个公司的总重量，总金额，总条数。如果有时间传入，就按时间查询，如果没有就查询全部；如果有公司传入，就按公司查询，如果没有就查询全部；如果有品种传入，就按品种查询，如果没有就查询全部
        //String plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<CompanyWeightMoneyNumExcelEn> list = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newId.put("chiCo", id.get("chiCo"));
                        newId.put("coSupplyChi", id.get("coSupplyChi"));
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为TruckExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        CompanyWeightMoneyNumExcelEn companyWeightMoneyNumExcelEn = CompanyWeightMoneyNumExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("totalWeight")) {
//                          String
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setTotalWeight(null);
                                } else {
                                    companyWeightMoneyNumExcelEn.setTotalWeight(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//                           trucksTrailerno
                            if (entry.getKey().equals("totalMoney")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setTotalMoney(null);
                                } else {
                                    companyWeightMoneyNumExcelEn.setTotalMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//                                trucksType
                            if (entry.getKey().equals("totalNum")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setTotalNum(null);
                                } else {
                                    companyWeightMoneyNumExcelEn.setTotalNum(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//                                trucksInfoApproved
                            if (entry.getKey().equals("product")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setProduct("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setProduct(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("chiCo")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setCo("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setCo(entry.getValue().toString());
                                }
                            }
//                                trucksPriv
                            if (entry.getKey().equals("co")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setCo("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setCo(entry.getValue().toString() + "." + companyWeightMoneyNumExcelEn.getCo());
                                }
                            }

//                                trucksEmiss
                            if (entry.getKey().equals("coSupply")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setCoSupply("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setCoSupply(entry.getValue().toString());
                                }
                            }
//                            给coSupply添加上chiCoSupply
                            if (entry.getKey().equals("coSupplyChi")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setCoSupply("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setCoSupply(companyWeightMoneyNumExcelEn.getCoSupply() + "." + entry.getValue().toString());
                                }
                            }
                        }
                        list.add(companyWeightMoneyNumExcelEn);
                    }
                    break;
                case "all":
                    CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
//        判空
                    if (coSupply != null && !coSupply.equals("")) {
                        companyWeightMoneyNumBO.setCoSupply(coSupply);
                    }
                    if (coSupplyChi != null && !coSupplyChi.equals("")) {
                        companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
                    }
                    if (co != null && !co.equals("")) {
                        companyWeightMoneyNumBO.setCo(co);
                    }
                    if (chiCo != null && !chiCo.equals("")) {
                        companyWeightMoneyNumBO.setChiCo(chiCo);
                    }
                    if (pdtClassId != null) {
                        QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                        cPdtClassQueryWrapper.eq("PDTCLASS_ID", pdtClassId);
//            PDTCLASS_DELETED=0
                        cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                        cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
                        List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                        List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getPdtclassId).collect(Collectors.toList());
                        QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                        cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                        List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                        companyWeightMoneyNumBO.setProduct(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
                    }
                    if (product != null && product.size() > 0) {
                        companyWeightMoneyNumBO.setProduct(product);
                    }
                    if (startTime != null && !startTime.equals("")) {
                        companyWeightMoneyNumBO.setStartTime(startTime);
                    }
                    if (endTime != null && !endTime.equals("")) {
                        companyWeightMoneyNumBO.setEndTime(endTime);
                    }
                    if (truckNo != null && !truckNo.equals("")) {
                        companyWeightMoneyNumBO.setTruckNo(truckNo);
                    }
                    if (iccardno != null && !iccardno.equals("")) {
                        companyWeightMoneyNumBO.setIccardno(iccardno);
                    }
                    if (isEmpty != null) {
                        companyWeightMoneyNumBO.setIsEmpty(isEmpty);
                    }
                    if (plantId != null && !plantId.equals("")) {
                        companyWeightMoneyNumBO.setPlantId(plantId);
                    }
                    if (iccardAlias !=null && !iccardAlias.equals("")) {
                        QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
                        queryWrapper3.eq("CARDINF_DELETED", 0);
                        queryWrapper3.eq("DATA_FROM", plantId);
                        queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
                        List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
                        if (cIcCardsInfos.size()>0) {
                            List<String> list2 = new ArrayList<>();
                            for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                                list2.add(cIcCardsInfo.getCardinfCardno());
                            }
                            companyWeightMoneyNumBO.setIccardnoAlias(list2);
                        }
                    }
//        默认为不查询空车
                    else {
                        companyWeightMoneyNumBO.setIsEmpty(0);
                    }
                    // 获取查询结果
                    List<CompanyWeightMoneyNumVo> companyWeightMoneyNumList = weightSheetsService.companyWeightMoneyNum(companyWeightMoneyNumBO);

// 计算合计值
                    BigDecimal totalWeight = BigDecimal.ZERO;
                    BigDecimal totalMoney = BigDecimal.ZERO;
                    BigDecimal totalNum = BigDecimal.ZERO;
                    for (CompanyWeightMoneyNumVo vo : companyWeightMoneyNumList) {
                        if (vo.getTotalWeight() != null) {
                            totalWeight = totalWeight.add(vo.getTotalWeight());
                        }
                        if (vo.getTotalMoney() != null) {
                            totalMoney = totalMoney.add(vo.getTotalMoney());
                        }
                        if (vo.getTotalNum() != null) {
                            totalNum = totalNum.add(vo.getTotalNum());
                        }
                    }

// 创建合计对象并添加到列表
                    CompanyWeightMoneyNumVo totalVo = new CompanyWeightMoneyNumVo();
                    totalVo.setCo("合计");
                    totalVo.setTotalWeight(totalWeight);
                    totalVo.setTotalMoney(totalMoney);
                    totalVo.setTotalNum(totalNum);
// 其他字段置空或保持默认值
// 将合计对象添加到结果列表中
                    companyWeightMoneyNumList.add(totalVo);
                    for (CompanyWeightMoneyNumVo companyWeightMoneyNumVo : companyWeightMoneyNumList) {
                        CompanyWeightMoneyNumExcelEn companyWeightMoneyNumExcelEn = CompanyWeightMoneyNumExcelEn.builder()
                                .totalWeight(companyWeightMoneyNumVo.getTotalWeight())
                                .totalMoney(companyWeightMoneyNumVo.getTotalMoney())
                                .totalNum(companyWeightMoneyNumVo.getTotalNum())
                                .product(Optional.ofNullable(companyWeightMoneyNumVo.getProduct()).map(Object::toString).orElse(""))
                                .co(Optional.ofNullable(companyWeightMoneyNumVo.getCo()).map(Object::toString).orElse("") + "." + Optional.ofNullable(companyWeightMoneyNumVo.getChiCo()).map(Object::toString).orElse(""))
                                .coSupply(Optional.ofNullable(companyWeightMoneyNumVo.getCoSupply()).map(Object::toString).orElse("") + "." + Optional.ofNullable(companyWeightMoneyNumVo.getCoSupplyChi()).map(Object::toString).orElse(""))
                                .build();
                        list.add(companyWeightMoneyNumExcelEn);
                    }
                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newId.put("chiCo", id.get("chiCo"));
                        newId.put("coSupplyChi", id.get("coSupplyChi"));
                        newIds.add(newId);
                    }
//             newIds为Map<String, Object>类型,需要转换为TruckExcelEn类型
                    for (Map<String, Object> newId : newIds) {
                        CompanyWeightMoneyNumExcelEn companyWeightMoneyNumExcelEn = CompanyWeightMoneyNumExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                            if (entry.getKey().equals("totalWeight")) {
//                          String
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setTotalWeight(null);
                                } else {
                                    companyWeightMoneyNumExcelEn.setTotalWeight(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//                           trucksTrailerno
                            if (entry.getKey().equals("totalMoney")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setTotalMoney(null);
                                } else {
                                    companyWeightMoneyNumExcelEn.setTotalMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//                                trucksType
                            if (entry.getKey().equals("totalNum")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setTotalNum(null);
                                } else {
                                    companyWeightMoneyNumExcelEn.setTotalNum(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//                                trucksInfoApproved
                            if (entry.getKey().equals("product")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setProduct("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setProduct(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("chiCo")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setCo("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setCo(entry.getValue().toString());
                                }
                            }
//                                trucksPriv
                            if (entry.getKey().equals("co")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setCo("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setCo(entry.getValue().toString() + "." + companyWeightMoneyNumExcelEn.getCo());
                                }
                            }

//                                trucksEmiss
                            if (entry.getKey().equals("coSupply")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setCoSupply("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setCoSupply(entry.getValue().toString());
                                }
                            }
                            //                            给coSupply添加上chiCoSupply
                            if (entry.getKey().equals("coSupplyChi")) {
                                if (entry.getValue() == null || entry.getValue().equals("")) {
                                    companyWeightMoneyNumExcelEn.setCoSupply("");
                                } else {
                                    companyWeightMoneyNumExcelEn.setCoSupply(companyWeightMoneyNumExcelEn.getCoSupply() + "." + entry.getValue().toString());
                                }
                            }
                        }
                        list.add(companyWeightMoneyNumExcelEn);
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(list);
    }


    @Async
    public Future<List<ProductWeightMoneyNumExportExcelEn>> productWeightMoneyNumExportExcel(List<Map<String, Object>> fields, List<Map<String, Object>> ids, String filename, Boolean isHeader, String mode, Boolean original, Map<String, String> pager, Integer pdtClassId, List<String> product, String startTime, String endTime, String truckNo, String icCardNo, String sheetName, HttpServletResponse response, String plantId, String coSupply, String coSupplyChi, String co, String coChi,
                                                                                             Integer isEmpty) {
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<ProductWeightMoneyNumExportExcelEn> list = new ArrayList<>();
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }

                    for (Map<String, Object> newId : newIds) {
                        ProductWeightMoneyNumExportExcelEn productWeightMoneyNumExportExcelEn = ProductWeightMoneyNumExportExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
//
                            if (entry.getKey().equals("product")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setProduct(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("totalMoney")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("totalNum")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//
                            if (entry.getKey().equals("totalWeight")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                        }
                        list.add(productWeightMoneyNumExportExcelEn);
                    }
                    break;
                case "all":
                    CompanyWeightMoneyNumBo companyWeightMoneyNumBO = new CompanyWeightMoneyNumBo();
//        判空
                /*    if (coSupply != null && !coSupply.equals("")) {
                        companyWeightMoneyNumBO.setCoSupply(coSupply);
                    }
                    if (coSupplyChi != null && !coSupplyChi.equals("")) {
                        companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
                    }
                    if (co != null && !co.equals("")) {
                        companyWeightMoneyNumBO.setCo(co);
                    }*/
                    if (coSupply != null && !coSupply.equals("")) {
                        companyWeightMoneyNumBO.setCoSupply(coSupply);
                    }
                    if (coSupplyChi != null && !coSupplyChi.equals("")) {
                        companyWeightMoneyNumBO.setCoSupplyChi(coSupplyChi);
                    }
                    if (co != null && !co.equals("")) {
                        companyWeightMoneyNumBO.setCo(co);
                    }
                    if (coChi != null && !coChi.equals("")) {
                        companyWeightMoneyNumBO.setChiCo(coChi);
                    }
                    if (pdtClassId != null) {
                        QueryWrapper<CPdtClass> cPdtClassQueryWrapper = new QueryWrapper<>();
                        cPdtClassQueryWrapper.eq("PDTCLASS_ID", pdtClassId);
//            PDTCLASS_DELETED=0
                        cPdtClassQueryWrapper.eq("PDTCLASS_DELETED", 0);
                        cPdtClassQueryWrapper.eq("DATA_FROM", plantId);
                        List<CPdtClass> cPdtClasses = icPdtClassService.list(cPdtClassQueryWrapper);
//            然后根据class查出所有PDTCLASS_ID与c_products_info表PDTINF_CLASS相同的数据
                        List cPdtClassIds = cPdtClasses.stream().map(CPdtClass::getPdtclassId).collect(Collectors.toList());
                        QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
                        cProductsInfoQueryWrapper.in("PDTINF_CLASS", cPdtClassIds);
                        List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
//            然后根据c_products_info表PDTINF_NAME全放到PdtName
                        companyWeightMoneyNumBO.setProduct(cProductsInfos.stream().map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList()));
                    }
                    if (product != null && product.size() > 0) {
                        companyWeightMoneyNumBO.setProduct(product);
                    }
                    if (startTime != null && !startTime.equals("")) {
                        companyWeightMoneyNumBO.setStartTime(startTime);
                    }
                    if (endTime != null && !endTime.equals("")) {
                        companyWeightMoneyNumBO.setEndTime(endTime);
                    }
                    if (truckNo != null && !truckNo.equals("")) {
                        companyWeightMoneyNumBO.setTruckNo(truckNo);
                    }
                    if (icCardNo != null && !icCardNo.equals("")) {
                        companyWeightMoneyNumBO.setIccardno(icCardNo);
                    }
                    if (plantId != null && !plantId.equals("")) {
                        companyWeightMoneyNumBO.setPlantId(plantId);
                    }
                    if(isEmpty != null){
                        companyWeightMoneyNumBO.setIsEmpty(isEmpty);
                    }
                    List<CompanyWeightMoneyNumVo> companyWeightMoneyNumList = weightSheetsService.productWeightMoneyNum(companyWeightMoneyNumBO);

// 计算合计值
                    // 计算合计值
                    BigDecimal totalWeight = BigDecimal.ZERO;
                    BigDecimal totalMoney = BigDecimal.ZERO;
                    BigDecimal totalNum = BigDecimal.ZERO;

                    for (CompanyWeightMoneyNumVo vo : companyWeightMoneyNumList) {
                        if (vo.getTotalWeight() != null) {
                            totalWeight = totalWeight.add(vo.getTotalWeight());
                        }
                        if (vo.getTotalMoney() != null) {
                            totalMoney = totalMoney.add(vo.getTotalMoney());
                        }
                        if (vo.getTotalNum() != null) {
                            totalNum = totalNum.add(vo.getTotalNum());
                        }
                    }
// 创建合计对象并添加到列表
                    CompanyWeightMoneyNumVo totalVo = new CompanyWeightMoneyNumVo();
                    totalVo.setTotalWeight(totalWeight);
                    totalVo.setTotalMoney(totalMoney);
                    totalVo.setTotalNum(totalNum);
// 其他字段置空或保持默认值
                    totalVo.setPdtClass(null);
                    totalVo.setProduct("合计");
                    totalVo.setCo(null);
                    totalVo.setChiCo(null);
                    totalVo.setCoSupply(null);
                    totalVo.setCoSupplyChi(null);

// 将合计对象添加到结果列表中
                    companyWeightMoneyNumList.add(totalVo);

                for (CompanyWeightMoneyNumVo companyWeightMoneyNumVo : companyWeightMoneyNumList) {
                        ProductWeightMoneyNumExportExcelEn companyWeightMoneyNumExportExcelEn = ProductWeightMoneyNumExportExcelEn.builder().build();
                        companyWeightMoneyNumExportExcelEn.setProduct(companyWeightMoneyNumVo.getProduct());
                        companyWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(String.valueOf(companyWeightMoneyNumVo.getTotalMoney())));
                        companyWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal((String.valueOf(companyWeightMoneyNumVo.getTotalNum()))));
                        companyWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal((String.valueOf(companyWeightMoneyNumVo.getTotalWeight()))));
                        list.add(companyWeightMoneyNumExportExcelEn);
                    }
                    break;


                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newIds.add(newId);
                    }

                    for (Map<String, Object> newId : newIds) {
                        ProductWeightMoneyNumExportExcelEn productWeightMoneyNumExportExcelEn = ProductWeightMoneyNumExportExcelEn.builder().build();
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
//
                            if (entry.getKey().equals("product")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setProduct(entry.getValue().toString());
                                }
                            }
//
                            if (entry.getKey().equals("totalMoney")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("totalNum")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalNum(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//
                            if (entry.getKey().equals("totalWeight")) {
                                if (entry.getValue() != null && !entry.getValue().equals("")) {
                                    productWeightMoneyNumExportExcelEn.setTotalWeight(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                        }
                        list.add(productWeightMoneyNumExportExcelEn);
                    }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AsyncResult<>(list);
    }


    public SzResult getWeightByDay(String company, String data, String plantId, String pdtInfStockcls, String token, String myPlantId) throws Exception {
        GetWeightByDayVo getWeightByDayVo = new GetWeightByDayVo();

        if (data != null && !data.isEmpty()) {
            getWeightByDayVo.setData(data);
        }
        getWeightByDayVo.setPlantId(myPlantId);

        if (pdtInfStockcls != null) {
            getWeightByDayVo.setPdtInfStockcls(Integer.valueOf(pdtInfStockcls));
        }

        List<GetWeightByDayVo> weightByDay = weightSheetsService.getWeightByDay(getWeightByDayVo);
        List<GetWeightByDayVo> weightByDaylist = new ArrayList<>();

        for (GetWeightByDayVo weightByDayVo : weightByDay) {
            String product = weightByDayVo.getProduct();
            QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
            cProductsInfoQueryWrapper.eq("PDTINF_PDT", product);
            if (pdtInfStockcls != null && !pdtInfStockcls.isEmpty()) {
                cProductsInfoQueryWrapper.eq("PDTINF_STOCKCLS", pdtInfStockcls);
            }
            List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
            if (!cProductsInfos.isEmpty()) {
                weightByDaylist.add(weightByDayVo);
            }
        }

        List<GetWeightByDayVo> list = new ArrayList<>();
        for (GetWeightByDayVo weightByDayVo : weightByDaylist) {
            GetWeightByDayVo listData = new GetWeightByDayVo();
            listData.setCompany(weightByDayVo.getCompany() + "." + weightByDayVo.getChiCo());
            listData.setProduct(weightByDayVo.getProduct());
            listData.setNumber(weightByDayVo.getNumber());
            listData.setUnitPrice(weightByDayVo.getUnitPrice());
            listData.setMoney(weightByDayVo.getMoney());
            listData.setTotalMoney(weightByDayVo.getTotalMoney());
            listData.setTotalNumber(weightByDayVo.getTotalNumber()); // 这里暂时不设置，稍后统一设置
            listData.setData(weightByDayVo.getData());
            listData.setPdtInfStockcls(weightByDayVo.getPdtInfStockcls());

            if (weightByDayVo.getPdtInfStockcls() == 1) {
                listData.setCompany(weightByDayVo.getSupplyCo() + "." + weightByDayVo.getSupplyChiCo());
            }
            list.add(listData);
        }

        // 计算每个product和company的totalNumber
        Map<String, BigDecimal> totalNumberMap = new HashMap<>();
        for (GetWeightByDayVo vo : list) {
            String key = vo.getProduct() + vo.getCompany();
            BigDecimal currentTotal = totalNumberMap.getOrDefault(key, BigDecimal.ZERO);
            BigDecimal number = new BigDecimal(vo.getNumber());
            totalNumberMap.put(key, currentTotal.add(number));
        }

        // 更新每个条目的totalNumber
        for (GetWeightByDayVo vo : list) {
            String key = vo.getProduct() + vo.getCompany();
            vo.setTotalNumber(totalNumberMap.get(key).toString());
        }

        // 使用map来合并具有相同product, company和data的数据
        Map<String, GetWeightByDayVo> map = new HashMap<>();
        for (GetWeightByDayVo obj : list) {
            String key = obj.getProduct() + obj.getCompany() + obj.getData();
            if (map.containsKey(key)) {
                GetWeightByDayVo existObj = map.get(key);
                existObj.setNumber(String.valueOf(new BigDecimal(existObj.getNumber()).add(new BigDecimal(obj.getNumber()))));
                existObj.setMoney(String.valueOf(new BigDecimal(existObj.getMoney()).add(new BigDecimal(obj.getMoney()))));
                map.put(key, existObj); // 更新map中的对象
            } else {
                map.put(key, obj);
            }
        }

        List<GetWeightByDayVo> resultList = new ArrayList<>(map.values());
        return SzResult.success(resultList);
    }
    public List<GetWeightByDayVo> getWeightByDayExcel(String company, String data, String plantId, String pdtInfStockcls, String token, String myPlantId) throws Exception {
        GetWeightByDayVo getWeightByDayVo = new GetWeightByDayVo();

        if (data != null && !data.isEmpty()) {
            getWeightByDayVo.setData(data);
        }
        getWeightByDayVo.setPlantId(myPlantId);

        if (pdtInfStockcls != null) {
            getWeightByDayVo.setPdtInfStockcls(Integer.valueOf(pdtInfStockcls));
        }

        List<GetWeightByDayVo> weightByDay = weightSheetsService.getWeightByDay(getWeightByDayVo);
        List<GetWeightByDayVo> weightByDaylist = new ArrayList<>();

        for (GetWeightByDayVo weightByDayVo : weightByDay) {
            String product = weightByDayVo.getProduct();
            QueryWrapper<CProductsInfo> cProductsInfoQueryWrapper = new QueryWrapper<>();
            cProductsInfoQueryWrapper.eq("PDTINF_PDT", product);
            if (pdtInfStockcls != null && !pdtInfStockcls.isEmpty()) {
                cProductsInfoQueryWrapper.eq("PDTINF_STOCKCLS", pdtInfStockcls);
            }
            List<CProductsInfo> cProductsInfos = icProductsInfoService.list(cProductsInfoQueryWrapper);
            if (!cProductsInfos.isEmpty()) {
                weightByDaylist.add(weightByDayVo);
            }
        }

        List<GetWeightByDayVo> list = new ArrayList<>();
        for (GetWeightByDayVo weightByDayVo : weightByDaylist) {
            GetWeightByDayVo listData = new GetWeightByDayVo();
            listData.setCompany(weightByDayVo.getCompany() + "." + weightByDayVo.getChiCo());
            listData.setProduct(weightByDayVo.getProduct());
            listData.setNumber(weightByDayVo.getNumber());
            listData.setUnitPrice(weightByDayVo.getUnitPrice());
            listData.setMoney(weightByDayVo.getMoney());
            listData.setTotalMoney(weightByDayVo.getTotalMoney());
            listData.setTotalNumber(weightByDayVo.getTotalNumber()); // 这里暂时不设置，稍后统一设置
            listData.setData(weightByDayVo.getData());
            listData.setPdtInfStockcls(weightByDayVo.getPdtInfStockcls());

            if (weightByDayVo.getPdtInfStockcls() == 1) {
                listData.setCompany(weightByDayVo.getSupplyCo() + "." + weightByDayVo.getSupplyChiCo());
            }
            list.add(listData);
        }

        // 计算每个product和company的totalNumber
        Map<String, BigDecimal> totalNumberMap = new HashMap<>();
        for (GetWeightByDayVo vo : list) {
            String key = vo.getProduct() + vo.getCompany();
            BigDecimal currentTotal = totalNumberMap.getOrDefault(key, BigDecimal.ZERO);
            BigDecimal number = new BigDecimal(vo.getNumber());
            totalNumberMap.put(key, currentTotal.add(number));
        }

        // 更新每个条目的totalNumber
        for (GetWeightByDayVo vo : list) {
            String key = vo.getProduct() + vo.getCompany();
            vo.setTotalNumber(totalNumberMap.get(key).toString());
        }

        // 使用map来合并具有相同product, company和data的数据
        Map<String, GetWeightByDayVo> map = new HashMap<>();
        for (GetWeightByDayVo obj : list) {
            String key = obj.getProduct() + obj.getCompany() + obj.getData();
            if (map.containsKey(key)) {
                GetWeightByDayVo existObj = map.get(key);
                existObj.setNumber(String.valueOf(new BigDecimal(existObj.getNumber()).add(new BigDecimal(obj.getNumber()))));
                existObj.setMoney(String.valueOf(new BigDecimal(existObj.getMoney()).add(new BigDecimal(obj.getMoney()))));
                map.put(key, existObj); // 更新map中的对象
            } else {
                map.put(key, obj);
            }
        }

        List<GetWeightByDayVo> resultList = new ArrayList<>(map.values());
        // 按照产品名和公司名排序
        resultList.sort(Comparator.comparing(GetWeightByDayVo::getProduct)
                .thenComparing(GetWeightByDayVo::getCompany));
        return resultList;
    }

}
